unit EditTck;

interface

uses
  Winapi.Windows, Winapi.Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, uniGUITypes, uniGUIAbstractClasses,
  uniGUIClasses, uniGUIForm, dxGDIPlusClasses, uniLabel, jpeg,
  uniGUIBaseClasses, uniImage, uniButton, uniBitBtn, DB, uniDateTimePicker,
  uniEdit, uniDBEdit, uniMultiItem, uniComboBox, uniDBComboBox, uniBasicGrid,
  uniDBGrid, uniPanel, uniMemo, uniDBMemo, DBClient, ClientRowSet,
  uniDBLookupComboBox, uniDBDateTimePicker;

type
  TFrmEditTck = class(TUniForm)
    UniImage8: TUniImage;
    lbCaption: TUniLabel;
    btSave: TUniBitBtn;
    btClose: TUniBitBtn;
    dsTck: TDataSource;
    lbCompTel: TUniLabel;
    edBillNo: TUniDBEdit;
    UniLabel1: TUniLabel;
    UniLabel5: TUniLabel;
    UniLabel2: TUniLabel;
    UniLabel3: TUniLabel;
    UniLabel4: TUniLabel;
    edDstNode: TUniDBComboBox;
    UniLabel6: TUniLabel;
    cbUnion: TUniDBComboBox;
    UniLabel7: TUniLabel;
    edDrvTel: TUniDBEdit;
    UniLabel8: TUniLabel;
    UniLabel9: TUniLabel;
    edAcptCont: TUniDBComboBox;
    UniLabel10: TUniLabel;
    edAcpTel: TUniDBEdit;
    UniPanel1: TUniPanel;
    UniPanel2: TUniPanel;
    UniLabel12: TUniLabel;
    edDriver: TUniDBEdit;
    UniLabel13: TUniLabel;
    edFee: TUniDBEdit;
    UniLabel14: TUniLabel;
    UniDBComboBox2: TUniDBComboBox;
    UniPanel3: TUniPanel;
    UniPanel4: TUniPanel;
    UniLabel15: TUniLabel;
    moSrvScope: TUniDBMemo;
    cbTckType: TUniDBComboBox;
    edBatchno: TUniDBEdit;
    UniLabel11: TUniLabel;
    cbTck: TUniDBComboBox;
    crsLine: TClientRowSet;
    crsLineLINE_ID: TStringField;
    crsLineLINE_TYPE: TStringField;
    crsLineLINE_CODE: TStringField;
    crsLineLINE_NAME: TStringField;
    crsLineGROUP_CODE: TStringField;
    crsLineGROUP_NAME: TStringField;
    crsLineSRC_NODE_NO: TStringField;
    crsLineSRC_NODE_NAME: TStringField;
    crsLineDST_NODE_NO: TStringField;
    crsLineDST_NODE_NAME: TStringField;
    crsLineSRC_CITY: TStringField;
    crsLineDST_CITY: TStringField;
    crsLineFETCH_FUNC: TStringField;
    crsLineDISP_FUNC: TStringField;
    crsLineLINE_FUNC: TStringField;
    crsLineOPT_FUNC: TStringField;
    crsLineINSURE_FUNC: TStringField;
    crsLineFEE_FUNC: TStringField;
    crsLineSYS_ACCOUNT: TStringField;
    crsLineVERSION: TSmallintField;
    cbSendNode: TUniDBLookupComboBox;
    dsNode: TDataSource;
    edLeveTime: TUniDateTimePicker;
    edArivTime: TUniDateTimePicker;
    crsNode: TClientRowSet;
    procedure btCloseClick(Sender: TObject);
    procedure btSaveClick(Sender: TObject);
    procedure UniFormShow(Sender: TObject);
    procedure cbTckCloseUp(Sender: TObject);
    procedure UniFormCreate(Sender: TObject);
  private
    { Private declarations }
    function isHaveDetail(truckNo: string): Boolean;
  public
    { Public declarations }
    procedure InitForm;
  end;

function FrmEditTck: TFrmEditTck;

implementation

{$R *.dfm}

uses
  MainModule, uniGUIApplication,dictmodule,expmodule, uCommFunction;

function FrmEditTck: TFrmEditTck;
begin
  Result := TFrmEditTck(UniMainModule.GetFormInstance(TFrmEditTck));
end;

procedure TFrmEditTck.btCloseClick(Sender: TObject);
begin
  TClientRowset(dsTck.DataSet).CancelUpdates;
  self.Close;
end;

procedure TFrmEditTck.btSaveClick(Sender: TObject);
begin
  if (edLeveTime.Text <> '') and (Pos('1899', edLeveTime.Text) < 1) then
    dsTck.DataSet.FieldByName('LEVE_TIME').AsString := edLeveTime.Text;
  if (edArivTime.Text <> '') and (Pos('1899', edArivTime.Text) < 1) then
    dsTck.DataSet.FieldByName('ARIV_TIME').AsString := edArivTime.Text;
  dsTck.DataSet.FieldByName('send_node_name').AsString := cbSendNode.Text;

  TClientRowset(dsTck.DataSet).ApplyUpdates;
  self.Close;
end;

procedure TFrmEditTck.cbTckCloseUp(Sender: TObject);
begin
  if dmdict.crsTruck.Locate('LICENSE',cbTck.Text,[]) then
  begin
    dsTck.DataSet.Edit;
    dsTck.DataSet.FieldByName('DRV_NAME').AsString := dmdict.crsTruck.FieldByName('DRV_NAME').AsString;
    dsTck.DataSet.FieldByName('DRV_TEL').AsString  := dmdict.crsTruck.FieldByName('DRV_TEL').AsString;
  end;
end;

procedure TFrmEditTck.InitForm;
var
  filter:String;
  I: Integer;
begin
  if not dsTck.DataSet.FieldByName('leve_time').IsNull then
    edLeveTime.Text := dsTck.DataSet.FieldByName('leve_time').AsString
  else
    edLeveTime.Text := '';
  if not dsTck.DataSet.FieldByName('ariv_time').IsNull then
    edArivTime.Text := dsTck.DataSet.FieldByName('ariv_time').AsString
  else
    edArivTime.Text := '';

  edBillNo.Enabled := True;
  if dsTck.DataSet.State = dsEdit then
    edBillNo.Enabled := not isHaveDetail(dsTck.DataSet.FieldByName('truck_no').AsString);

  cbTck.Items.Clear;
  for I := 1 to dmDict.crsTruck.RecordCount do
  begin
    dmDict.crsTruck.RecNo := I;
    cbTck.Items.Add(dmDict.crsTruck.FieldByName('license').AsString);
  end;

  cbSendNode.Enabled := dmExpress.UserInfo.IsCenter;
end;



function TFrmEditTck.isHaveDetail(truckNo: string): Boolean;
begin
  with dmExpress.crsExec do
  begin
    Close;
    CommandText := 'select count(1) cnt from TSP_TRANCE where GROUP_CODE=' + QuotedStr(dmExpress.UserInfo.GroupCode) +
      ' and TRUCK_NO=' + QuotedStr(truckNo);
    Open;
    Result := FieldbyName('cnt').AsInteger > 0;
  end;
end;

procedure TFrmEditTck.UniFormCreate(Sender: TObject);
begin
  SetCompoentLanguage(Self);
  crsNode.Data := dmDict.crsNode.Data;
end;

procedure TFrmEditTck.UniFormShow(Sender: TObject);
begin
  InitForm;
end;

end.
